<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Data proposal',
				width: 450,
				height: 475,
				closed: true,
				cache: false,
				href: base_url+'proposal/add',
				modal: true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'proposal/save/add';
		}
		// end newData
		
		editData = function (id){
			//var row = $('#dg').datagrid('getSelected');
			//if (row){
				$('#dialog').dialog({
					title: 'Edit Data proposal',
					width: 450,
					height: 475,
					closed: true,
					cache: false,
					href: base_url+'proposal/edit/'+id,
					modal: true
				});
				
				$('#dialog').dialog('open');  
				url = base_url+'proposal/save/edit';
			// }
		}
		//end editData
		
		detail = function (id){
			//var row = $('#dg').datagrid('getSelected');
			//if (row){
				$('#dialogDetail').dialog({
					title: 'Detail Data proposal',
					width: 450,
					height: 475,
					closed: true,
					cache: false,
					href: base_url+'proposal/detail/'+id,
					modal: true
				});
				
				$('#dialogDetail').dialog('open');  
				url = base_url+'proposal/save/edit';
			// }
		}
		//end detail
		
		deleteData = function (id){
			//var row = $('#dg').datagrid('getSelected');
			//if(row){
				if(confirm("Apakah yakin akan menghapus data?")){
					var response = '';
					$.ajax({ type: "GET",
						 url: base_url+'proposal/delete/' + id,
						 async: false,
						 success : function(response){
							var response = eval('('+response+')');
							if (response.success){
								$.messager.show({
									title: 'Success',
									msg: 'Data Berhasil Dihapus'
								});
								// reload and close tab
								$('#dg').datagrid('reload');
							} else {
								$.messager.show({
									title: 'Error',
									msg: response.msg
								});
							}
						 }
					});
				}
			//}
		}
		//end deleteData 
		
		saveData = function(){
			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						$('#dialog').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');		// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		//end saveData
		
		actionbutton = function(value, row, index){
			var col="";
			//if (row.kd_fakultas != null) {
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>27, 'policy'=>'DETAIL'))){?>
				col += '<a href="#" onclick="detail(\''+row.id+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Detail</a>';
				<?}?>
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>27, 'policy'=>'EDIT'))){?>
				col += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="editData(\''+row.id+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Edit</a>';
				<?}?>
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>27, 'policy'=>'DELETE'))){?>
				col += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="deleteData(\''+row.id+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Delete</a>';
				<?}?>
			//}
			return col;
		}
		
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>proposal/grid"});
		});
			
		// onchange
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
		});
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				kd_fakultas : $('#s_kd_fakultas').val(),
				kd_prodi : $('#s_kd_prodi').val(),
				tahun : $('#s_tahun').val(),
			});
			//$('#dg').datagrid('enableFilter');
		}
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
			buttons:[
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>27, 'policy'=>'ADD'))){?>
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					}
				<?}?>			
					]
			
			});			
		});
	});
</script>

<table id="dg" title="Kelola Data Proposal" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			">
	<thead>
		<tr>
			<th field="tgl_daftar2" sortable="true" width="135">Tanggal Daftar</th>
			<th field="nim" sortable="true" width="110">NIM</th>
			<th field="nama" sortable="true" width="165">Nama</th>
			<th field="judul_proposal" sortable="true" width="200">Judul Proposal</th>
			<th field="proposal" sortable="true" width="100">Status</th>
			<th field="action" align="center" formatter="actionbutton" width="150">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<!--div style="margin-bottom:5px">
		<a href="#" onclick="newData();" class="easyui-linkbutton" iconCls="icon-add" plain="true">Add</a>
	</div-->
	<div>
		<table width="700" border="0">
		  <tr>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="s_kd_fakultas" style="width:290px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Tahun Akademik</td>
			<td>: 
				<select id="s_tahun" name="s_tahun" style="width:100px;">
					<?=$this->mdl_prosedur->OptionTahunakademikUp();?>
				</select>
			</td>
		  </tr>
		  <tr>
			<td>Program Studi</td>
			<td>: 
				<select id="s_kd_prodi" name="s_kd_prodi" style="width:290px;">
					<!--option value="">-- Pilih --</option-->
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton" iconCls="icon-search">Filter</a></td>
		  </tr>
		</table>
	</div>
</div>
